//
//  XPLoginViewController.m
//  QQ
//
//  Created by nhope on 2017/4/14.
//  Copyright © 2017年 xiaopin. All rights reserved.
//

#import "XPLoginViewController.h"
#import "XPLoginSettingView.h"

@interface XPLoginViewController ()<NSTextFieldDelegate>

@property (weak) IBOutlet NSImageView *logoImageView;
@property (weak) IBOutlet NSTextField *qqTextField;
@property (weak) IBOutlet NSTextField *passwordTextField;

@end

@implementation XPLoginViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // 圆形Logo
    [self.logoImageView setWantsLayer:YES];
    self.logoImageView.layer.cornerRadius = 50.0;
    self.logoImageView.layer.masksToBounds = YES;
    self.logoImageView.layer.borderWidth = 1.0;
    self.logoImageView.layer.borderColor = [NSColor grayColor].CGColor;
    // 背景色
    [self.view setWantsLayer:YES];
    self.view.layer.backgroundColor = [rgbColor(248.0, 248.0, 248.0) CGColor];
}

- (void)viewWillAppear {
    [super viewWillAppear];
    // 设置widnow相关属性
    [self.view.window setTitleVisibility:NSWindowTitleHidden];
    [self.view.window setTitlebarAppearsTransparent:YES];
    [self.view.window setMovable:YES];
    [self.view.window setMovableByWindowBackground:YES];
    [[self.view.window standardWindowButton:NSWindowZoomButton] setHidden:YES];
    [[self.view.window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
    [[self.view.window standardWindowButton:NSWindowCloseButton] setHidden:YES];
    [self.view.window setRestorable:NO];
    [self.view.window center];
}

- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

#pragma mark - <NSTextFieldDelegate>

/// 拦截`回车键`事件
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
    if (commandSelector == @selector(insertNewline:)) { // Press enter key.
        NSTextField *textField = (NSTextField *)control;
        BOOL isQQNumber = isQQ(self.qqTextField.stringValue);
        if (textField == self.qqTextField) {
            if (isQQNumber) {
                [self.passwordTextField becomeFirstResponder];
            }
        } else if (textField == self.passwordTextField) {
            if (isQQNumber) {
                [self loginButtonAction:nil];
            } else {
                [self.qqTextField becomeFirstResponder];
            }
        }
        return YES;
    }
    return NO;
}

#pragma mark - Actions

- (IBAction)closeButtonAction:(NSButton *)sender {
    [self.view.window close];
}

- (IBAction)loginButtonAction:(NSButton *)sender {
    NSString *qq = [self.qqTextField.stringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.passwordTextField.stringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (!isQQ(qq) || password.length < 6) {
        NSAlert *alert = [[NSAlert alloc] init];
        alert.messageText = @"登录失败";
        alert.informativeText = @"账号或密码错误，请重新输入。";
        [alert beginSheetModalForWindow:self.view.window completionHandler:nil];
        return;
    }
    
    // TODO:登录验证 & 登录成功后的处理
    [self.view.window close];
    [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessNotification object:nil];
}

- (IBAction)settingButtonAction:(NSButton *)sender {
    if (self.view.window.childWindows.count) {
        for (NSWindow *window in self.view.window.childWindows) {
            [self.view.window removeChildWindow:window];
        }
        return;
    }

    NSRect rect = NSMakeRect(self.view.window.frame.origin.x,
                             self.view.window.frame.origin.y-75.0,
                             self.view.window.frame.size.width,
                             80.0);
    NSWindow *window = [[NSWindow alloc] init];
    [window setFrame:rect display:YES animate:YES];
    window.titlebarAppearsTransparent = YES;
    window.contentView = [XPLoginSettingView settingViewFromXib];
    [self.view.window addChildWindow:window ordered:NSWindowBelow];
}

@end
